<?php
class DbAbstractForeign {
	var $container = null;
	var $foreignKey = null;
	var $srcKey = null;
	var $hostClass;
	var $foreignClass;
	var $id = '';
	
	function DbAbstractForeign($host, $foreign, $foreignKey=null) {
		$this->hostClass = $host;
		$this->foreignClass = $foreign;
		if ($foreignKey==null) {
			$this->foreignKey = DbAbstractTable::_get_FOREIGN($this->hostClass, $this->foreignClass);
		} else {
			$this->foreignKey = $foreignKey;
		}
		//if ($src==null) {
			$cl = getInstanceByClass($this->foreignClass);
			$this->srcKey = $cl->_get_ID();
		//} else {
			//$this->srcKey = $src;
		//}
	}
	
	function &getForeign() {
		if (!isset($this->container) && ($this->id>0 || strlen($this->id)>1)) {
			$criteria = $this->srcKey."='".$this->id."'";
			$this->container = DbAbstractTable::__select($criteria, 1, 0, null, $this->foreignClass);
		}
		return $this->container;
	}
	
	function setForeign(&$foreign) {
		$this->container = &$foreign;
	}
	
	function getForeignId() {
		return $this->id;
	}
	
	function &getList() {
		if (!isset($this->container)) {
			//$criteria = DbAbstractTable::_get_FOREIGN($this->foreignClass,$this->hostClass)."='".$this->id."'";
			$criteria = $this->foreignKey."='".$this->id."'";
			$this->container = DbAbstractTable::__select($criteria, 0, 0, null, $this->hostClass);
		}
		return $this->container;
	}
	
	function setList(&$list) {
		$this->container = &$list;
	}
	
	function getId() {
		return $this->id;
	}
	
	function setId($id) {
		$this->id = $id;
	}
	
	function getForeignKey() {
		return $this->foreignKey;
	}

	function getSrcKey() {
		return $this->srcKey;
	}
}
?>